<?php
function success($data = [])
{
	if(!is_array($data)){
		return false;
	}

	$returnData = [
		'code' => '100',
		'message' => 'ok',
		'data' => $data,
	];

	ajax_return($returnData);
}

function fail($code = 0,$message = ''){
	$returnData = [
		'code' => strval($code),
		'message' => $message,
		'data' => [],
	];

	ajax_return($returnData);
}

function ajax_return($returnData,$force = false)
{
	if(!is_array($returnData)){
		return false;
	}
	if(C('DEV') && $force){
		return json_encode($returnData,JSON_UNESCAPED_UNICODE);
	}else{
		header('Content-Type:application/json; charset=utf-8');
		exit(json_encode($returnData,JSON_UNESCAPED_UNICODE));
	}
}


//生成随机字符串
function nonceStr(){
    //定义一个字符串
    $chars='ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    //当前的时间戳
    $time = time();
    //从该字符串中获取随机字符串
    $chars = $chars.$time;
    //打乱字符串
    $chars = str_shuffle($chars);
    return substr($chars,0,16);
}

function signature($time,$url,$nonceStr){
    //拼接字符串
    $string = 'jsapi_ticket='.getJsapiTicket().'&noncestr='.$nonceStr.'&timestamp='.$time.'&url='.$url;
    //生成签名
    $string = sha1($string);
    return $string;
}


//生成使用微信JS-SDK需要注入的配置信息,参数$url(当前网址的url)
function getConfig($url){
    $array = [];
    $array['debug'] = true;
    $array['appId'] = C('appID');
    //生成签名的时间戳
    $time = time();
    $array['timestamp'] = $time;
    //签名使用的随机字符串
    $nonceStr = nonceStr();
    $array['nonceStr'] = $nonceStr;
    $array['signature'] = signature($time,$url,$nonceStr);
    //设置需要使用的js接口
    $array['jsApiList'] = ['chooseImage'];
    //返回配置信息
    return json_encode($array);
 }